package com.sense.kukoo.common.anim

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.sense.kukoo.lib_logger.LoggerManager

class BottomPlayerAnimHelper {

    private val TAG = BottomPlayerAnimHelper::class.simpleName
    private val defAnimateDuration = 400L

    private var mMaxTranslationY:Float = 0F
    private var mFlagScrolling: Boolean? = null
    private var mMoveY:Float = 0F
    private lateinit var playerView: View

    fun setPlayerView(v:View){
        playerView = v
    }

    fun setMaxTranslationY(max:Float){
        mMaxTranslationY = max
    }

    fun animateToShow(){
        val translationY: Float = playerView.translationY
        if(translationY < 16){
            playerView.translationY = 0F
            return
        }
        val duration = (translationY * defAnimateDuration) / mMaxTranslationY
        playerView.animate()
            .translationY(0F)
            .setDuration(duration.toLong())
            .start()
    }

    fun animateToHide(){
        val translationY: Float = playerView.translationY
        val moveY = mMaxTranslationY - translationY
        if(moveY < 16){
            playerView.translationY = mMaxTranslationY
            return
        }
        val duration = (moveY * defAnimateDuration) / mMaxTranslationY
        playerView.animate()
            .translationY(mMaxTranslationY)
            .setDuration(duration.toLong())
            .start()
    }

    fun dispatchScrollEvent(moveY:Int, scrollState:Int){
        //LoggerManager.d(TAG, "dispatchScrollEvent moveY=${moveY}, scrollState=${scrollState}")
        var translationY: Float = playerView.translationY
        translationY = translationY + moveY
        translationY = if(translationY < 0) 0F else translationY
        translationY = if(translationY > mMaxTranslationY) mMaxTranslationY else translationY
        playerView.translationY = translationY
        //
        mMoveY += moveY
        //
        if(mFlagScrolling == null){
            mFlagScrolling = true
            mMoveY = 0F
        } else {
            if(scrollState == RecyclerView.SCROLL_STATE_IDLE){
                mFlagScrolling = null
                animateToShow()
            }
        }
    }
}